package com.atguigu.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author admin
 * <p>
 * 编写一个程序，开启三个线程，这三个线程的 ID 分别为 A、B、C，每个线程都将自己的 ID 在屏幕上打印 10 编，
 * 要求输出的结果必须按顺序显示。
 * 如：ABCABCABC...... 依次递归
 */
public class TestABCAlternate {

    public static void main(String[] args) {
        AlternateDemo alternateDemo = new AlternateDemo();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    alternateDemo.loopA();
                }
            }
        }, "A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    alternateDemo.loopB();
                }
            }
        }, "B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    alternateDemo.loopC();
                }
            }
        }, "C").start();
    }
}

class AlternateDemo {

    /*
    当前正在执行线程的标记
     */
    private int number = 1;

    private Lock lock = new ReentrantLock();

    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    public void loopA() {
        lock.lock();

        try {
            /*
            1.判断
             */
            if (number != 1) {
                try {
                    condition1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            /*
            2.打印
             */
            for (int i = 1; i <= 1; i++) {
                System.out.println("A");
            }

            /*
            3.唤醒
             */
            number = 2;
            condition2.signal();

        } finally {
            lock.unlock();
        }
    }

    public void loopB() {

        lock.lock();

        try {
            /*
            1.判断
             */
            if (number != 2) {
                try {
                    condition2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            /*
            2.打印
             */
            for (int i = 1; i <= 1; i++) {
                System.out.println("B");
            }

            /*
            3.唤醒
             */
            number = 3;
            condition3.signal();

        } finally {
            lock.unlock();
        }
    }

    public void loopC() {

        lock.lock();

        try {
            /*
            1.判断
             */
            if (number != 3) {
                try {
                    condition3.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            /*
            2.打印
             */
            for (int i = 1; i <= 1; i++) {
                System.out.println("C");
            }

            /*
            3.唤醒
             */
            number = 1;
            condition1.signal();

        } finally {
            lock.unlock();
        }
    }

}
